<div class="layui-form seller-form"  action="" >
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">订单编号：</label>
            <div class="layui-input-inline seller-inline-3">
                <input type="text" name="bill_bn"  placeholder="请输入订单编号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">日期：</label>
            <div class="layui-input-inline seller-inline-3">
                <input type="text" name="date" class="layui-input" id="test1">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm"  lay-submit lay-filter="payments-search"><i class="iconfont icon-chaxun"></i>筛选</button>
        </div>
    </div>
</div>
<div class="table-body">
    <div class="layui-tab layui-tab-brief" lay-filter="tab">
        <ul class="layui-tab-title">
            <li class="layui-this" lay-id="no" data-handle="0">未出单</li>
            <li lay-id="all">所有埋单</li>
        </ul>
    </div>
    <table id="paymentsSellerTable" lay-filter="paymentsSellerTable"></table>
</div>
<script>
    layui.use(['table','form','layer', 'laydate', 'element'], function(){
        let lock = false;
        var table = layui.table,form = layui.form,layer = layui.layer;
        //第一个实例
        layui.laydate.render({
            elem: '#test1' //指定元素
        });
        table.render({
            elem: '#paymentsSellerTable', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-260',
            cellMinWidth: '80',
            page:true,
            limit:70,
            id:'paymentsSellerTable',
            url: "{:url('')}",
            where: {'type': 'no'},
            cols: [[ //表头
                {type:'numbers'},
                {field: 'bill_bn',title:'订单编号',align:'center',minWwidth:150},
                {field: 'room_bn',title: '房间编号',align:'center',minWidth:120},
                {field: 'nickname', title: '用户',align:'center',minWidth:150, templet: function(data){
                    return '<span>'+data.user.nickname+'</span>'; 
                }},
                {field: 'amount', title: '金额',align:'center',minWidth:120},
                {field: 'pay_status', title: '状态',align:'center',width:100,templet: function(data){
                    switch(data.pay_status){
                        case '0':
                            return '<span>未支付</span>';
                        case '1':
                            return '<span>已支付</span>';
                        case '2':
                            return '<span>已取消</span>'; 
                        default: 
                            return '<span>非法</span>'; 
                    }
                }},
                {field: 'status', title: '出单状态',align:'center',width:100,templet: function(data){
                    if(data.status) {
                        return '<span>已出单</span>';
                    } else {
                        return '<span>未出单</span>';
                    }
                }},
                {field: 'ctime', title: '订单时间',align:'center',width:170, templet: function(data){
                    var timestamp4 = new Date(data.ctime*1000);
                    return timestamp4.toLocaleDateString().replace(/\//g, "-") + " " + timestamp4.toTimeString().substr(0, 8); 
                }},
                {fixed: 'right', width:170,title:'操作', align:'center', toolbar: '#payments'}
            ]]
        });
        form.on('submit(payments-search)', function(data){
            table.reload('paymentsSellerTable', {
                where: data.field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        
        layui.element.on('tab(tab)', function(){
            var types = this.getAttribute('lay-id');
            table.reload('paymentsSellerTable', {
                where: {type: types}
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false;
        });

        //监听工具条
        table.on('tool(paymentsSellerTable)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if(layEvent == 'cancel'){
                layer.confirm('真的要取消吗',{icon: 3,title:'提示'}, function(){
                    JsPost("{:url('cancel')}",{id: data.id}, function(res) {
                        if(res.status){
                            layer.msg(res.msg,{time:1300},function(){
                                table.reload('paymentsSellerTable');
                            });
                        }else{
                            layer.msg(res.msg);
                        }
                    });
                });
            } else if(layEvent == 'dy') {
                $.ajax({
                    url:"{:url('getPayBill')}",
                    type:'post',
                    data: {id:data.id},
                    success:function(res){
                        if(res.status == true){
                            let date = new Date();
                            let year = date.getFullYear();
                            let month = date.getMonth() + 1;
                            let day = date.getDate();
                            let hour = date.getHours();
                            let minute = date.getMinutes();

                            let bill = res.data;
                            console.log(bill);

                            LODOP = getLodop(document.getElementById('LODOP1'), document.getElementById('LODOP_EM1'));
                            LODOP.PRINT_INITA(0, 0, 230, 230, "打印条码");
                            LODOP.SET_PRINTER_INDEX(-1);
                            LODOP.SET_PRINT_PAGESIZE(1,580,-1,"");
                            LODOP.SET_PRINT_STYLE("FontSize", 12);
                            LODOP.SET_PRINT_STYLE("Bold", 1);
                            LODOP.ADD_PRINT_TEXT("30","30","230","20","会员卡后结买单");
                            LODOP.SET_PRINT_STYLE("FontSize", 12);
                            LODOP.SET_PRINT_STYLE("Bold", 1);
                            LODOP.ADD_PRINT_TEXT(70, 10, 300, 20, "买单时间：" + bill.ctime2);
                            LODOP.ADD_PRINT_TEXT(100, 10, 230, 20, "房间编号：" + bill.room_bn);
                            LODOP.ADD_PRINT_TEXT(130, 10, 230, 20, "买单人：" + bill.user.nickname);
                            LODOP.SET_PRINT_STYLE("FontSize", 10);
                            LODOP.SET_PRINT_STYLE("Bold", 1);
                            /*if(booking.taocan && typeof (booking.taocan) == 'object'){
                                LODOP.ADD_PRINT_TEXT(90, 10, 230, 20, "套餐：" + booking.taocan['name'] + "  -  ￥" + toDecimal2NoZero(Number(booking.price )));
                            }else{
                                LODOP.ADD_PRINT_TEXT(90, 10, 230, 20, "套餐：-");
                            }*/
                            LODOP.ADD_PRINT_TEXT(150, 10, 230, 20, "会员卡号：" + bill.user.mobile);
                            let x = 180
                            LODOP.SET_PRINT_STYLE("FontSize", 10);
                            LODOP.SET_PRINT_STYLE("Bold", 0);
                            
                            /*let nk = "";
                            if(strlen(liquor.liquor.goods_name) <= 24){
                                let y = 24 - strlen(liquor.liquor.goods_name);
                                for(let a = 0; a < y; a++){
                                    nk += " ";
                                }
                            }else{
                                liquor.liquor.goods_name = liquor.liquor.goods_name.substring(0,21) + "..";
                                nk += "  ";
                            }*/
                            //nk += "  ";
                           // LODOP.ADD_PRINT_TEXT(x, 10, 230, 20,  liquor.liquor.goods_name + nk + "x" + liquor.number);
                            x += 20
                            LODOP.SET_PRINT_STYLE("FontSize", 12);
                            LODOP.SET_PRINT_STYLE("Bold", 1);
                            LODOP.ADD_PRINT_TEXT(x + 10, 10, 230, 20, "买单金额：￥" + toDecimal2NoZero(Number(bill.amount)));

                            LODOP.SET_PRINT_STYLE("FontSize", 9);
                            LODOP.SET_PRINT_STYLE("Bold", 0);
                            //LODOP.ADD_PRINT_TEXT(x + 30, 10, 230, 20, "打印时间：" + year + "/" + month + "/"+ day + " " + hour + ":"+ minute);
                            LODOP.SET_PRINT_STYLE("FontSize", 10);
                            LODOP.SET_PRINT_STYLE("Bold", 1);
                            //LODOP.ADD_PRINT_TEXT(x + 60, 10, 230, 20, "支付方式：无");
                            LODOP.SET_PRINT_STYLE("FontSize", 8);
                            LODOP.SET_PRINT_STYLE("Bold", 0);
                            LODOP.ADD_PRINT_TEXT(x + 90, 10, 230, 20, "-----------------------------------------");
                            LODOP.PREVIEW();
                        }else{
                            layer.msg(res.msg);
                        }
                    }
                },'json');
            } else if(layEvent == 'cd') {
                $.ajax({
                    url:"{:url('update')}",
                    type:'post',
                    data: {id:data.id},
                    success:function(res){
                        layer.msg('出单成功',{time:1300},function(){
                            table.reload('paymentsSellerTable');
                        });
                    }
                })
            } 
        });
        function toDecimal2NoZero(x) {
            let f = Math.round(x * 100) / 100;
            let s = f.toString();
            return s;
        }

        function strlen(str){
            let len = 0;
            for (let i=0; i<str.length; i++) {
                let c = str.charCodeAt(i);
                //单字节加1
                if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
                    len++;
                }
                else {
                    len+=2;
                }
            }
            return len;
        }
    });
</script>

<script type="text/html" id="payments">
    {{# if( d.status != '1' ) { }}
    <a class="layui-btn layui-btn-xs" lay-event="cd">出单</a>
    {{# } }}
    <a class="layui-btn layui-btn-xs" lay-event="dy">打印</a>
    {{# if(d.iscancel && d.status != '1' ) { }}
    <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancel">取消</a>-->
    {{# } }}
</script>